package com.ninth_group.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ninth_group.dto.CityDTO;
import com.ninth_group.exception.city.CityException;
import com.ninth_group.result.R;
import com.ninth_group.service.TFHDataService;
import com.ninth_group.vo.TFHDataVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 24小时天气数据控制类
 */
@RestController
@RequestMapping("/24hData")
public class TFHDataController {
    @Resource
    private TFHDataService TFHDataService;

    /**
     * 获取城市24小时天气数据
     * @param cityDTO 城市信息
     * @return 24小时天气数据
     */
    @PostMapping("/c")
    public R getTwentyHcData(@RequestBody CityDTO cityDTO) throws JsonProcessingException {
        List<TFHDataVO> cityTwentyHoursData =
                TFHDataService.getCityTwentyHcData(cityDTO);
        return R.ok("查询成功", cityTwentyHoursData);
    }

    /**
     * 获取省会24小时天气数据
     * @param pName 省份名称
     * @return 24小时天气数据
     */
    @GetMapping("/p/{pName}")
    public R getTwentyHpData(@PathVariable String pName) throws JsonProcessingException {
        List<TFHDataVO> cityTwentyHoursData =
                TFHDataService.getCityTwentyHpData(pName);
        return R.ok("查询成功", cityTwentyHoursData);
    }

    @ExceptionHandler(CityException.class)
    public R handleCityException(CityException e) {
        return R.error(e.getMessage());
    }
}
